package main

import (
	"fmt"
)

/**
 *  for 循环示例
 */
func main() {
	forDemo_01()
	forDemo_02()
	forDemo_03()
	forEachDemo()
}

/**
 *  for 的第一种方式
 * for init; condition; post { }
 */
func forDemo_01() {

	for i := 1; i < 10; i++ {
		for j := 1; j <= i; j++ {
			fmt.Printf("%d * %d = %d \t", i, j, i*j)
		}
		fmt.Println()
	}
}

/**
 *  for循环第二种方式 类似while循环
 *  for condition
 */
func forDemo_02() {
	sum := 1
	for sum < 10 {
		sum += sum
	}
	fmt.Println(sum)

}

/**
 * for {}
 * 类似java while循环
 */
func forDemo_03() {
	sum := 1
	result := 0
	for {
		sum++
		if sum%2 == 1 {
			continue
		}
		result += sum

		if sum >= 100 {
			break
		}
	}
	fmt.Println("100 以内的偶数和", result)
}

/**
 * foreach range 操作
 */
func forEachDemo() {
	strs := []string{"abc", "def", "ghi"}
	for i, s := range strs {
		fmt.Println(i, s)
	}
}
